from django.core.management.base import BaseCommand
from product.service.product_snapshot import ProductSnapshotService

class Command(BaseCommand):
    help = '创建每日商品库存快照'

    def handle(self, *args, **options):
        try:
            self.stdout.write(self.style.SUCCESS('开始创建商品库存快照...'))
            
            # 创建快照服务实例并执行
            snapshot_service = ProductSnapshotService()
            snapshots = snapshot_service.create_daily_snapshot()
            
            self.stdout.write(
                self.style.SUCCESS(f'成功创建库存快照：共 {len(snapshots)} 条记录')
            )
            
        except Exception as e:
            self.stdout.write(
                self.style.ERROR(f'创建库存快照失败: {str(e)}')
            )